package sbc.corso.shareable;

import java.util.Vector;

import corso.lang.CorsoConnection;
import corso.lang.CorsoData;
import corso.lang.CorsoDataException;
import corso.lang.CorsoException;
import corso.lang.CorsoShareable;
import corso.lang.CorsoVarOid;

public class PlatinenFabrikInfo implements CorsoShareable {
	
    private CorsoVarOid platinenOid;
    private CorsoVarOid sensorenOid;


    /**====================================================================* 
     * constructor: leere Info
     *=====================================================================*/  
    public PlatinenFabrikInfo() {
    	platinenOid = new CorsoVarOid ();
    	sensorenOid = new CorsoVarOid ();
    }


    /**====================================================================* 
     * constructor: Info einlesen von OID
     *=====================================================================*/  
    public PlatinenFabrikInfo(CorsoVarOid oid) {
        this();
        try {
            //System.out.println ("Einlesen der PlatinenFabrik Info");
            oid.readShareable (this, null, CorsoConnection.NO_TIMEOUT);
        }
        catch (Exception e) {
            System.out.println (e);
            e.printStackTrace();
            System.exit (-5);
        }
    }


    /**====================================================================* 
     * constructor: neue Fabrik Info anlegen 
     *=====================================================================*/  
    public PlatinenFabrikInfo(CorsoConnection con) {
        try {
        	platinenOid = con.createVarOid (con.getCurrentStrategy());
        	sensorenOid = con.createVarOid (con.getCurrentStrategy());

            Platinen platinen = new Platinen(con);
            Sensoren sensoren = new Sensoren(con);

            try {
            	platinenOid.writeShareable (platinen, CorsoConnection.INFINITE_TIMEOUT);
            	sensorenOid.writeShareable (sensoren, CorsoConnection.INFINITE_TIMEOUT);
            }
            catch (CorsoException e2) {
                System.out.println (e2);
                System.exit (-6);
            }
        }
        catch (CorsoException e1) {   
            System.out.println (e1);
            System.exit (-7);
        }
    }


    /**====================================================================* 
     * constructor: einlesen der Info aus bestehender PLatinenfabrik
     *=====================================================================*/  
    public PlatinenFabrikInfo(String fabrikName, String site, 
                          CorsoConnection con)
    {
    	platinenOid = new CorsoVarOid ();
    	sensorenOid = new CorsoVarOid ();

        // get the named object for Fabrik
        CorsoVarOid namedOid = new CorsoVarOid ();
        
        try {
            namedOid = con.getNamedVarOid (fabrikName,
                                           site,
                                           null,
                                           false,
                                           CorsoConnection.NO_TIMEOUT);

            // read FabrikInfo
            namedOid.readShareable (this, null, CorsoConnection.NO_TIMEOUT);
        }
        catch (CorsoException e1) {
            System.exit (-8);
        } 
    }
    
    public void addPlatine(CorsoConnection con, Platine platine) {
	    Platinen platinen = new Platinen(platinenOid);
	    //System.out.println("da");
	    
	    platinen.addPlatine(con, platine);
	    try {
	    	//System.out.println("platine.getFirstSensor().getName()" + platine.getFirstSensorOid().getName());
	    	platinenOid.writeShareable (platinen, CorsoConnection.INFINITE_TIMEOUT);
	    }	    
		catch (CorsoException ce) {
			ce.printStackTrace();
		}
    }
    
    public void addSensor(CorsoConnection con, Sensor sensor) {
    	Sensoren sensoren = new Sensoren(sensorenOid);
    	sensoren.addSensor(con, sensor);
	    try {
	    	sensorenOid.writeShareable (sensoren, CorsoConnection.INFINITE_TIMEOUT);
	    }	    
		catch (CorsoException ce) {
			ce.printStackTrace();
		}
    }
    
    
    public Vector getPlatinenOids () {
        Platinen platinen = new Platinen(platinenOid);   
        return platinen.getPlatinenOids();
    }
    
    
    public Vector getSensorenOids () {
    	Sensoren sensoren = new Sensoren(sensorenOid);  
        return sensoren.getSensorenOids();
    }

    
	public void read(CorsoData data) throws CorsoDataException {
		StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);
        if (arity != 2)
            throw new CorsoDataException ();
        data.getShareable (platinenOid);
        data.getShareable (sensorenOid);
	}

	
	public void write(CorsoData data) throws CorsoDataException {
        data.putStructTag ("fabrikinfo", 2);
        data.putShareable (platinenOid);
        data.putShareable (sensorenOid);
	}


	public CorsoVarOid getPlatinenOid() {
		return platinenOid;
	}


	public void setPlatinenOid(CorsoVarOid platinenOid) {
		this.platinenOid = platinenOid;
	}


	public CorsoVarOid getSensorenOid() {
		return sensorenOid;
	}


	public void setSensorenOid(CorsoVarOid sensorenOid) {
		this.sensorenOid = sensorenOid;
	}
}
